<?php

/**
 * @package Framework03
 * @author Walter Tamboer
 */

/**
 * Base class for objects in this framework.
 */
class Object
{
	/**
	 * @var array An array with cached Model objects.
	 */
	private static $LoadedModels;
	/**
	 * @var array An array with cached Helper objects.
	 */
	private static $LoadedHelpers;
	/**
	 * @var CookieJar The cookie jar.
	 */
	private static $CookieJar;
	/**
	 * @var SessionJar The session jar.
	 */
	private static $SessionJar;
	/**
	 * @var Acl The access control list of this framework.
	 */
	private static $Acl;
	/**
	 * Initializes a new instance of this class.
	 */
	public function __construct()
	{
	}
	/**
	 * Cleans up all resources used by this class.
	 */
	public function __destruct()
	{
	}
	/**
	 * Loads the cookie jar.
	 * @return CookieJar Returns the cookie jar instance.
	 */
	protected function LoadCookieJar()
	{
		if (!isset(self::$CookieJar))
			self::$CookieJar = new CookieJar;

		return self::$CookieJar;
	}
	/**
	 * Loads the session jar.
	 * @return SessionJar Returns the session jar instance.
	 */
	protected function LoadSessionJar()
	{
		if (!isset(self::$SessionJar))
			self::$SessionJar = new SessionJar;

		return self::$SessionJar;
	}
	/**
	 * Loads the Access Control List.
	 * @return Acl Returns the access control list.
	 */
	protected function LoadAcl()
	{
		if (!isset(self::$Acl))
			self::$Acl = new Acl;

		return self::$Acl;
	}
	/**
	 * Loads a model or returns the one that's present in cache.
	 * @param string $Name The name of the model to load.
	 * @return Model Returns the requested model object.
	 */
	protected function LoadModel($Name)
	{
		if (!isset(self::$LoadedModels))
			self::$LoadedModels = array();

		if (array_key_exists($Name, self::$LoadedModels))
			return self::$LoadedModels[$Name];

		$Path = dirname(__FILE__) . '/../models/' . $Name . '.php';

		if (!is_file($Path))
			throw new Exception("Trying to load a model named '" . $Name . "' which is not found!", 404);

		require_once($Path);

		$Class = $Name . 'Model';

		$Object = new $Class;

		self::$LoadedModels[$Name] = $Object;

		return $Object;
	}
	/**
	 * Loads a helper or returns the one that's present in cache.
	 * @param string $Name The name of the helper to load.
	 * @return Helper Returns the requested helper object.
	 */
	protected function LoadHelper($Name)
	{
		if (!isset(self::$LoadedHelpers))
			self::$LoadedHelpers = array();

		if (array_key_exists($Name, self::$LoadedHelpers))
			return self::$LoadedHelpers[$Name];

		$Path = dirname(__FILE__) . '/../helpers/' . $Name . '.php';

		if (!is_file($Path))
			throw new Exception("Trying to load a helper named '" . $Name . "' which is not found!", 404);

		require_once($Path);

		$Class = $Name . 'Helper';

		$Object = new $Class;

		self::$LoadedHelpers[$Name] = $Object;

		return $Object;
	}
}

?>